package services;

import dao.ArticleInfoDao;
import models.ArticleInfo;
import models.vo.ArticleInfoVO;
import utils.ResultJSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

@WebServlet("/init")
public class InitServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int succ = -1; //succ为1表示操作成功
        String msg = "";
        ArticleInfoVO articleInfoVO = null;  //将articleinfoVO放在外面
        //1.从前端获取参数
        int id = Integer.parseInt(req.getParameter("id"));  //文章的id
        //2.调用数据库执行相应的业务逻辑
        if(id > 0){
            ArticleInfoDao articleInfoDao = new ArticleInfoDao();
            try {
                articleInfoVO = articleInfoDao.getArticleById(id); //进行查询
                succ = 1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else{
            msg = "无效的参数";
        }
        //3.将操作的结果返回给前端
        HashMap<String,Object> result = new HashMap<>();
        result.put("succ",succ);
        result.put("msg",msg);
        result.put("art",articleInfoVO);  //将上面的articleInfoVO传入
        ResultJSONUtils.write(resp,result);

    }
}
